<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i> tab选项卡</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-tabs v-model="message">
                <el-tab-pane :label="`未读消息(${unread.length})`" name="first">
                    <el-table :data="unread" :show-header="false" style="width: 100%">
                        <el-table-column>
                            <template slot-scope="scope">
                                <span class="message-title">您的申请单号：{{scope.row.applyNo}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="reviewStatusName" width="180"></el-table-column>
                        <el-table-column width="120">
                            <template slot-scope="scope">
                                <el-button size="small" @click="handleRead(scope.row.id)">标为已读</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="handle-row" v-if="unread.length>0">
                        <el-button type="primary" @click="handleAllRead">全部标为已读</el-button>
                    </div>
                </el-tab-pane>
                <el-tab-pane :label="`已读消息(${read.length})`" name="second">
                    <template v-if="message === 'second'">
                        <el-table :data="read" :show-header="false" style="width: 100%">
                            <el-table-column>
                                <template slot-scope="scope">
                                    <span class="message-title">您的申请单号：{{scope.row.applyNo}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="reviewStatusName" width="180"></el-table-column>
                            <!--<el-table-column width="120">-->
                                <!--<template slot-scope="scope">-->
                                    <!--<el-button type="danger" @click="handleDel(scope.$index)">删除</el-button>-->
                                <!--</template>-->
                            <!--</el-table-column>-->
                        </el-table>
                        <!--<div class="handle-row" v-if="read.length>0">-->
                            <!--<el-button type="danger">删除全部</el-button>-->
                        <!--</div>-->
                    </template>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>

<script>
import {applyMessage,updateApplyMessage} from "../axios/url";
import http from "../axios/fetch";

    export default {
        data() {
            return {
                message: 'first',
                showHeader: false,
                applyList: [],
                read: [],
                recycle: [],
                unread: [],

            }
        },
        mounted() {
            this.getApplyMessage();
        },
        methods: {
            //单条消息为已读
            handleRead(id) {
                this.updateApplyMessage(id)

            },
            //全部消息为已读
            handleAllRead(){
                let list = [];
                this.applyList.forEach((item) => {
                    if(item.read===0)
                    list.push(item.id)
                });
                this.updateApplyMessage(list.toLocaleString())
            },
            handleDel(index) {
                const item = this.read.splice(index, 1);
                this.recycle = item.concat(this.recycle);
            },
            handleRestore(index) {
                const item = this.recycle.splice(index, 1);
                this.read = item.concat(this.read);
            },
            getApplyMessage() {
                try{
                    http.post(applyMessage).then(res => {
                        if(res.data && res.data.resultMess === "success") {
                            this.read = [];
                            this.unread = [];
                            this.applyList = res.data.data.applyMessageVOs;
                            this.applyList.forEach((item) => {
                                if(item.read === 1){
                                    this.read.push(item)
                                }
                                else {
                                    this.unread.push(item)
                                }
                            });
                        }
                    })
                } catch (e){
                    this.$notify.error({
                        title: '提示',
                        message: '网络错误',
                    });
                }
            },
            //修改消息为已读
            updateApplyMessage(id) {
                try{
                    http.post(updateApplyMessage,{ids:id}).then(res => {
                        if(res.data && res.data.resultMess === "success") {
                            this.getApplyMessage()
                        }
                    })
                } catch (e){
                    this.$notify.error({
                        title: '提示',
                        message: '网络错误',
                    });
                }
            }
        },
        computed: {
            unreadNum(){
                return this.applyList.length;
            }
        }
    }

</script>

<style>
.message-title{
    cursor: pointer;
}
.handle-row{
    margin-top: 30px;
}
</style>

